<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="Clone"
    >
      <!-- 表单 -->
      <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
        <el-form-item label="经纪">
          <el-select
            v-model="formData.agent"
            style="width: 50%"
            placeholder="请选择"
          >
            <el-option v-for="item in brokerList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <!-- 分类 -->
        <el-form-item label="分类">
          <el-select
            v-model="formData.category"
            style="width: 50%"
            placeholder="请选择"
          >
            <el-option v-for="item in classifyTitle" :key="item.id" :value="item.id" :label="item.title" />
          </el-select>
        </el-form-item>
        <!-- 标 题 -->
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="formData.title"
            style="width: 50%"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <!-- 标签 -->
        <el-form-item label="标签" prop="username">
          <el-tag
            v-for="tag in formData.tags"
            :key="tag"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="inputValue"
            class="input-new-tag"
            size="small"
            @blur="handleInputConfirm"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>

        </el-form-item>

        <el-form-item label="地址">
          <el-row type="flex">
            <!-- 省份大全 -->
            <el-select v-model="map.province" placeholder="请选择" @change="changeaddress">
              <el-option v-for="item in provinces" :key="item" :value="item" :label="item" />
            </el-select>
            <!-- 区 -->
            <el-select v-model="map.district" placeholder="请选择" @change="districtChange">
              <el-option v-for=" (item,index) in districtList" :key="index" :value="item.name" :label="item.name"></el-option>
            </el-select>
            <!-- 县 -->
            <el-select v-model="map.county" placeholder="请选择" @change="countyChange">
              <el-option v-for="(item,index) in countyList" :key="index" :value="item.name" :label="item.name"></el-option>
            </el-select>
          </el-row>
        </el-form-item>
        <!-- 详细地址 -->
        <el-form-item label="详细地址">
          <el-input v-model="formData.address" placeholder="地址" />
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="formData.price" placeholder="请输入价格" />
        </el-form-item>
        <!-- 描述 -->
        <el-form-item label="描述">
          <el-input v-model="formData.desc" type="textarea" />
        </el-form-item>
        <!-- 内容 -->
        <h4 class="neirong">内容</h4>
        <!-- 富文本内容 -->
        <el-form-item>
          <vue-editor v-model="formData.content" />
        </el-form-item>
        <el-form-item class="photo">
          <h4>介绍图片</h4>
          <updateimgVue ref="photoTwo" />
        </el-form-item>
        <el-form-item class="photo">
          <h4>封面</h4>
          <updateFlies ref="photoONE" />
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="Clone">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 文本
import { VueEditor } from 'vue2-editor'
// 封面
import updateFlies from './updateFiles.vue'
import updateimgVue from './updateimg.vue'
// 图片
// import updateimgVue from './updateimg.vue'
import { addbroker, getedithouser, edithouser, classify, getbroker } from '@/api/houserMange'
import { get } from './api'
export default {
  components: {
    updateFlies,
    updateimgVue,
    VueEditor
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 省份大全
      provinces: ['北京', '天津', '上海', '重庆', '新疆', '西藏', '宁夏', '内蒙古', '广西', '黑龙江', '吉林', '辽宁', '河北', '山东', '江苏', '安徽', '浙江', '福建', '广东', '海南', '云南', '贵州', '四川', '湖南', '湖北', '河南', '山西', '陕西', '甘肃', '青海', '江西', '台湾', '香港', '澳门'],
      // 校验
      rules: {
        title: [{ required: true, message: '请输入标题', trigge: 'blur' }, { min: 2, max: 20, message: '请输入2-20的标题', trigge: 'blur' }]
      },
      // 下拉框地址 默认为空  地图显示
      map: {
        // address: '',
        province: '', // 省
        district: '', // 区
        county: '' //  县
      },
      content: '<h1>Some initial content</h1>',
      formData: {
        title: '',
        desc: '',
        image: '',
        agent: '',
        content: '',
        address: ``,
        price: '',
        photos: [],
        tags: [],
        category: ''

      },

      // 标签
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      classifyTitle: [], // 分类
      brokerList: [], // 经纪
      districtList: [], // 区列表
      countyList: [] // 县
    }
  },
  async created () {
    // 渲染分类
    this.classifyTitle = await classify()
    // 获取经纪
    this.brokerList = await getbroker()
  },
  methods: {
    // 省下框事件
    async changeaddress() {
      // 再次选中给他清空
      this.map.district = ''
      this.map.county = ''
      // console.log(this.address)
      const res = await get({
        subdistrict: this.map.province
      })
      // 区
      this.districtList = res.data.districts[0].districts
      // 添加给 formdata
      this.formData.address = this.map.province
      // console.log(8989, districtList)
    },
    // 区下来框事件
    async districtChange() {
      this.map.county = ''
      const res = await get({
        subdistrict: this.map.district
      })
      this.countyList = res.data.districts[0].districts
      // 添加给formdata
      this.formData.address = this.map.province + this.map.district
      console.log(555, this.formData.address)
    },
    // 县下拉框事件
    async countyChange() {
      this.formData.address = this.map.province + this.map.district + this.map.county
      // console.log(this.map.county)
      // console.log(this.formData.address)
    },

    // 标签
    // 删除
    handleClose(tag) {
      console.log(tag)
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue === '') {
        this.$message.warning('标签不能空')
        return
      }
      if (inputValue) {
        console.log(inputValue)
        // this.formData.push(inputValue)
        this.dynamicTags.push(inputValue)
        console.log(this.dynamicTags)
      }
      this.formData.tags.push(inputValue)
      this.inputVisible = false
      this.inputValue = ''
    },
    // 图片上传
    // 取消
    Clone() {
      this.$emit('update:dialogVisible', false)
      this.formData = {
        title: '',
        desc: '',
        image: '',
        agent: '',
        content: '',
        address: '',
        price: '',
        photos: [],
        tags: [],
        category: ''
      }
      // 清空地图下来框
      this.map = {
        province: '', // 省
        district: '', // 区
        county: '' //  县
      }
      this.$refs.form.resetFields()
      this.$refs.photoONE.removeList()
      this.$refs.photoTwo.removeList()
    },
    // 编辑回显
    async editHuixian(id) {
      this.formData = await getedithouser(id)
      console.log(this.formData)
      // this.$refs.photoONE.fileList.push({ url: this.formData.image })
      // 回显封面
      this.$refs.photoONE.fileList.push({
        url: this.formData.image
      })
      // 隐藏进度条 100进度条 一百就隐藏
      this.$refs.photoONE.percentage = 100
      // 图片
      this.formData.photos.forEach(item => {
        this.$refs.photoTwo.fileList.push({ url: item })
      })
      this.$refs.photoTwo.percentage = 100
      // this.$refs.photoTwo.fileList.push({ url: this.formData.image })
    },

    // 确定添加
    async confirm() {
      // 编辑
      if (this.formData.id) {
        // console.log('编辑')
        // this.$refs.form.va
        if (!this.$refs.photoONE.fileList[0]) {
          this.$message.warning('图片不能空')
          return
        }

        this.formData.image = this.$refs.photoONE.fileList[0].url
        // 图片
        const photo = this.$refs.photoTwo.fileList
        this.formData.photos = photo.map(item => item.url)
        await edithouser(this.formData)
      } else {
        // 添加
        if (!this.$refs.photoONE.fileList[0] || this.formData.image) {
          this.$message.warning('图片不能空')
          return
        }
        this.formData.image = this.$refs.photoONE.fileList[0].url

        const photo = this.$refs.photoTwo.fileList
        // let photoList=[]
        photo.forEach(item => {
          this.formData.photos.push(item.url)
        })
        await addbroker(this.formData)
      }

      // console.log(res)
      // 更新列表
      this.$emit('update')
      // 清空照片上传
      this.Clone()
    }

  }
}
</script>

<style scoped>
.neirong{
  margin-left: 10px;
}
.photo{
  display: flex
}
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
